# import sys
#
# import cv2
# from PyQt5.QtCore import QTimer
# from PyQt5.QtGui import QImage, QPixmap
# from PyQt5.QtWidgets import QApplication, QWidget, QMessageBox
#
# from form import Ui_Form
#
#
# class MyMainWindow(QWidget):
#     def __init__(self):
#         super().__init__()
#         self.ui = Ui_Form()
#         self.ui.setupUi(self)
#
#         self.timer = None
#         self.video_capture = None
#
#         self.Init()
#         self.destroyed.connect(self.release_camera)
#
#     def Init(self):
#         # 启动相机
#         self.video_capture = cv2.VideoCapture('rtsp://localhost:8554/live')
#
#         if not self.video_capture.isOpened():
#             QMessageBox.critical(self, "错误", "无法打开视频源，请检查RTSP地址是否正确")
#             return
#
#         # 连接刷新事件
#         self.timer = QTimer(self)
#         self.timer.timeout.connect(self.update_frame)
#         self.timer.start(16)  # 更新帧的时间间隔（以毫秒为单位）
#
#     def update_frame(self):
#         ret, frame = self.video_capture.read()
#         if ret:
#             # 显示帧
#             rgb_image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
#             h, w, ch = rgb_image.shape
#             bytes_per_line = ch * w
#             qt_image = QImage(rgb_image.data, w, h, bytes_per_line, QImage.Format_RGB888)
#             pixmap = QPixmap.fromImage(qt_image)
#
#             # 在QLabel上显示帧
#             self.ui.label.setPixmap(pixmap)
#
#     def release_camera(self):
#         if self.video_capture is not None:
#             self.video_capture.release()
#             self.video_capture = None
#
#
# def main():
#     app = QApplication(sys.argv)
#     window = MyMainWindow()
#     window.show()
#     sys.exit(app.exec_())
#
#
# if __name__ == "__main__":
#     main()
#
# # ffmpeg -re -stream_loop -1 -i "C:\Users\hongz\Desktop\abc\video_ornament_yangjian.mp4" -c copy -f rtsp rtsp://localhost:8554/live


import sys

from PyQt5.QtCore import QUrl
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout
from PyQt5.QtMultimedia import QMediaPlayer, QMediaContent
from PyQt5.QtMultimediaWidgets import QVideoWidget


class RTMPPlayer(QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)
        self.setWindowTitle("RTMP Player")
        self.setGeometry(100, 100, 800, 600)

        self.video_widget = QVideoWidget(self)
        self.player = QMediaPlayer(None, QMediaPlayer.VideoSurface)
        self.player.setVideoOutput(self.video_widget)

        layout = QVBoxLayout()
        layout.addWidget(self.video_widget)
        self.setLayout(layout)

        self.play_stream("http://127.0.0.1:8080/show/stream.m3u8")

    def play_stream(self, stream_url):
        media_content = QMediaContent(QUrl(stream_url))
        self.player.setMedia(media_content)
        self.player.play()


if __name__ == "__main__":
    app = QApplication(sys.argv)
    player = RTMPPlayer()
    player.show()
    sys.exit(app.exec_())

